<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="序号" prop="sortNum">
        <el-input-number v-model="dataForm.sortNum" placeholder="序号"></el-input-number>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称" @change="genCode"></el-input>
      </el-form-item>
      <el-form-item label="代码" prop="code">
        <el-input v-model="dataForm.code" placeholder="代码"></el-input>
      </el-form-item>
      <el-form-item label="是否系统" prop="isSys" >
        <el-radio v-model="dataForm.isSys" :disabled="dataForm.isSys==1&&dataForm.id!=0"  label="1">是</el-radio>
        <el-radio v-model="dataForm.isSys" :disabled="dataForm.isSys==1&&dataForm.id!=0"  label="0">否</el-radio>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/def/fieldGroup'
    import {genPinYinCode} from "@/util/util";

    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    id: 0,
                    code: '',
                    name: '',
                    status: '',
                    sortNum: '',
                    isSys: ''
                },
                dataRule: {
                    code: [
                        {required: true, message: '代码不能为空', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '名称不能为空', trigger: 'blur'}
                    ],

                    isSys: [
                        {required: true, message: '是否系统不能为空', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.id = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        getObj(this.dataForm.id).then(response => {
                            this.dataForm = response.data.data
                        })
                    }
                })
            },
            genCode() {
                this.dataForm.code = genPinYinCode(this.dataForm.name);
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        if (this.dataForm.id) {
                            putObj(this.dataForm).then(data => {
                                this.$message.success('修改成功')
                                this.visible = false
                                this.$emit('refreshDataList')
                            });
                        } else {
                            addObj(this.dataForm).then(data => {
                                this.$message.success('添加成功')
                                this.visible = false
                                this.$emit('refreshDataList')
                            })
                        }
                    }
                })
            }
        }
    }
</script>
